//
//  UserInfoManager.m
//  WheatPlan
//
//  Created by 郑渊文 on 6/9/15.
//  Copyright (c) 2015 IOSTeam. All rights reserved.
//

#import "UserInfoManager.h"

@implementation UserInfoManager


@synthesize currUser;

static UserInfoManager *_shareInstance;
// 取单例
+(UserInfoManager *)shareInstance
{
    @synchronized ([UserInfoManager class])
    {
        if (_shareInstance == nil)
        {
            _shareInstance = [[UserInfoManager alloc] init];
        }
    }
    return _shareInstance;
}
-(UserModel *)currUser
{
    if(currUser == nil)
        currUser = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePathName]];
    return currUser;
}
-(id)init
{
    if (self = [super init])
    {
        // 解档
        currUser = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePathName]];
    }
    return self;
}

// 得到沙盒文件路径
-(NSString *)getFilePathName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePathName = [documentsDirectory stringByAppendingPathComponent:@"User.atchive"];
    return filePathName;
}

// 保存新用户
-(void)saveUser:(UserModel*)newUser
{
    currUser = newUser;
    // 归档
    [NSKeyedArchiver archiveRootObject:currUser toFile:[self getFilePathName]];
}
// 用户退出
-(void)logout
{
    currUser = nil;
    
    // 归档
    [NSKeyedArchiver archiveRootObject:currUser toFile:[self getFilePathName]];
    
}

@end
